#!/usr/bin/env bash

bool() {
    case "$1" in
    Y* | y* | true | TRUE | 1) return 0 ;;
    esac
    return 1
}

if [ $(whoami) != 'azuracast' ]; then
    echo 'This script must be run as the "azuracast" user. Rerunning...'
    exec gosu azuracast azuracast_restore "$@"
fi

echo "AzuraCast Backup Restore"

APPLICATION_ENV="${APPLICATION_ENV:-production}"
echo "(Environment: $APPLICATION_ENV)"

if [ "$APPLICATION_ENV" = "production" ]; then
    if bool "$COMPOSER_PLUGIN_MODE"; then
        composer update --no-dev --optimize-autoloader
    fi
else
    composer install
fi

exec azuracast_cli azuracast:restore "$@"
